package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miplus.ccrm.common.*;
import com.miplus.ccrm.core.controller.vo.conf.UserInfoReqVo;
import com.miplus.ccrm.core.controller.vo.conf.UserInfoResVo;
import com.miplus.ccrm.core.controller.vo.cust.CaseResVo;
import com.miplus.ccrm.core.mapper.SysUserMapper;
import com.miplus.ccrm.core.pojo.SysUser;
import com.miplus.ccrm.core.service.ExportService;
import com.miplus.ccrm.core.service.crd.FiImportHistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
* @author pc
* @description 针对表【sys_user(系统用户表)】的数据库操作Service实现
* @createDate 2024-02-22 20:28:53
*/
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    @Resource
    private ExportService exportService;

    @Resource
    private FiImportHistService fiImportHistService;
    private IPage<UserInfoResVo> systemUserSelect(UserInfoReqVo paras, String usrid)
    {
        return baseMapper.systemUserSelect(new Page<>(paras.getPageNum(), paras.getPageSize()),usrid,paras);
    }

    @Override
    public CmdResVo expUserInfo(JSysUser user, UserInfoReqVo paras, HttpServletResponse response)
    {
        IPage<UserInfoResVo> pages = systemUserSelect(paras,user.getUsrid());
        ArrayList arr=new ArrayList();

        String exptempid=JPubUtils.getExpTempID(paras.getExpfilename(),"用户列表");

        String filename=JPubUtils.getExpFileName(
                paras.getExpfilename(),exptempid
        );
        pages.convert(u->{
            HashMap map=JPubUtils.toExportMap(u);
            if(map!=null) arr.add(map);
            return u;
        });
        CmdResVo resVo=exportService.ToExcel(
                response,
                arr,
                paras.getExpidlist(),
                paras.getExpnamelist(),
                filename,
                user.getUsrid()
        );

        int okcount=resVo.getCount();
        if(okcount>0){
            fiImportHistService.AddExportHist(
                    resVo.getKeyId(),
                    user.getUsrid(),
                    exptempid,
                    filename,
                    okcount
            );
        }
        return resVo;
    }
}




